<?php

namespace app\controller\admin;

use app\base\BaseEncryptController;
use app\model\agency\AgencyGoodsModel;
use app\model\agency\AgencyModel;
use app\model\goods\GoodsModel;
use think\Request;

/**
 * AgentGoods 代理商商品
 * Class AgentGoods
 * @package app\controller
 */
class AgencyGoods extends BaseEncryptController
{
    private $agency_model;

    protected function initialize()
    {
        parent::initialize();
        $this->model = new AgencyGoodsModel();
        $this->agency_model = new AgencyModel();

    }

    /**
     * 分页列表
     * @param int $id
     * @param string $status
     * @param string $keyword
     * @param int $page
     * @param int $limit
     * @return \think\response\Json
     */
    public function getPage($id, $status = '', $keyword = '', $page = 1, $limit = 10)
    {
        $map = [
            'agency_id' => $id
        ];
        if (!empty($status)) {
            $map['status'] = $status;
        }
        $mapLike = [];
        if (!empty($keyword)) {
            $mapLike = [
                'g.goods_title' => "%" . $keyword . "%"
            ];
        }
        $data = $this->model->getPageAgencyList($map, $mapLike, 'a.sort DESC', $page, $limit);
        $agency = $this->agency_model->getInfo($id);
        return self::returnMsg(1, [
            'other' => $agency,
            'totalCount' => $data['total'],
            'currentPage' => $data['current'],
            'pageList' => $data['dataList']
        ]);
    }

    /**
     * 改变代理商商品库存
     * @param Request $request
     * @return \think\response\Json
     * @throws \Exception
     */
    public function onChange(Request $request)
    {
        $formData = [
            'id' => $request->param('id'),
            'change' => $request->param('change'),
            'storage' => $request->param('storage')
        ];
        $validate = 'app\validate\AgencyGoodsValidate.change';
        $result = $this->validate($formData, $validate);
        if (true !== $result) {
            return self::returnMsg(991, [], $result);
        } else {
            if (empty($formData['id'])) {
                return self::returnMsg(0);
            }
            $agency_goods = $this->model->getInfo($formData['id']);
            $goods_model = new GoodsModel();
            $storage = [
                'storage' => $formData['storage'],
                'target_id' => $agency_goods['agency_id'],
            ];
            $operate = [
                'operate' => 'agency',
                'admin_id' => $this->currentUser['user_id']
            ];
            if ($formData['change'] == 'add') {
                $remark = "代理商商品增加库存";
            } else {
                $remark = "代理商商品减少库存";
            }
            $ret = $goods_model->changeStore($agency_goods['goods_id'], $storage, $formData['change'], 0, $operate, $remark);
            if (false === $ret) {
                return self::returnMsg(0);
            } else {
                return self::returnMsg(1);
            }
        }
    }

    /**
     * 保存
     * @param Request $request
     * @return \think\response\Json
     */
    public function onSave(Request $request)
    {
        $formData = [
            'agency_id' => $request->param('agency_id'),
            'goods_id' => $request->param('goods_id'),
            'goods_storage' => $request->param('goods_storage')
        ];
        $validate = 'app\validate\AgencyGoodsValidate.add';
        $result = $this->validate($formData, $validate);
        if (true !== $result) {
            return self::returnMsg(991, [], $result);
        } else {
            $formData['admin_id'] = $this->currentUser['user_id'];
            $ret = $this->model->addGoods($formData);
            if (false === $ret) {
                return self::returnMsg(0);
            } else {
                return self::returnMsg(1);
            }
        }
    }

    /**
     * 删除
     * @param $id
     * @return \think\response\Json
     */
    public function onDelete($id)
    {
        $flag = $this->model->delGoods($id, $this->currentUser['user_id']);
        if (false !== $flag) {
            $this->model->delRedis($id);
            $this->model->redis->zRem($this->model->redis_key . ':rank', $id);
            $this->model->addToRecycle($id, 'AgencyGoodsModel', '代理商商品管理', $this->currentUser['user_id'], $this->currentUser['user_type'],'agency');
            return self::returnMsg(1);
        } else {
            return self::returnMsg(1002);
        }
    }


}
